package classes
{
	
	import flash.display.BitmapData;
	
	public class AnalyzedData
	{
		public  var bitmapdata:BitmapData;
		
		public  var matrix:classes.Matrix;
		
		[Bindable]
		public  var columns:Array = new Array();
		
		public function AnalyzedData(bitmapdata:BitmapData){
			this.bitmapdata = bitmapdata.clone();	
			this.matrix = classes.Matrix.fromBitmapData(this.bitmapdata);
			
		}
		

		public static function calculateMean(matrix:classes.Matrix):Array{
			var ret:Array = new Array(matrix.width);
			for(var i:int=0;i<matrix.width;i++) ret[i] = 0;
			
			for(var y:int=0;y<matrix.height;y++){
				for (var x:int=0;x<matrix.width;x++){
					ret[x]+=matrix.data[x][y]/matrix.height;
				}
			}
			
			return ret;
		}
		public static function diff(ar:Array, h:int=1):Array{
			var s:Array = new Array();
			for(var i:int = h;i<ar.length;i++){
				if(h==1)
					s[i-1] = (ar[i-1]-ar[i]);
				else
					s[i-h] = (ar[i-h]-ar[i])/h;
			}
			return s;
		} 
		public static function findZeros(ar:Array,inv:Boolean=false):Array{
			var ret:Array = new Array();
			for (var i:int=1;i<ar.length-1;i++){
				var a:Number = ar[i-1], b:Number=ar[i], c:Number=ar[i+1];
				if(inv){ 
					a=-a;b=-b;c=-c;
				}
				if(a<=0 && /*a<b && b<c && */c>0){
//					trace(i,a,b,c,"<<<");
					a = Math.abs(a);
					b = Math.abs(b);
					c = Math.abs(c);
					var idx:int = i-1;
					var min:Number = a;
					if(b<min){idx = i;min = b;}
					if(c<min){idx = i+1;}
							
					ret.push(idx);
					i+=2;
				}else{
//					trace(i,a,b,c);
				}
			}
			return ret;
			
		}
		public static function cleanupZeros(ar:Array):Array{
			//TODO: Maciek chce zmienic tu cos ale sam nie wie co...
			var diffs:Array = ar.map(function(item:*, idx:int, array:Array):*{
				return item - array[idx-1];
			});
			diffs.sort(Array.NUMERIC);
			diffs = diffs.slice(diffs.length*0.1,diffs.length*0.9);
			var diff:int = 0;
			diffs.map(function(item:*, idx:int, array:Array):void{diff += item});
			diff /= diffs.length;
			diff *= 0.7;
						 
			ar = ar.filter(function(item:*,idx:int,array:Array):Boolean{
				if(idx==0) return true;
				return item-diff > array[idx-1];
			}); 
			return ar;
/*
			var ret:Array = new Array();
			for(var i:int=1;i<ar.length-1;i++){
				var a:int = ar[i-1], b:int = ar[i], c:int = ar[i+1];
				if(c-a<diff*1.8){
					if(diff-(c-a)>diff-(b-a)){
						//wywal c
						//przepisz b
						ret.push(b);
					}else{
						//wywal b
						//przepisz c
						ret.push(c);
					}
					i+=2;
				}else{
					ret.push(a);
				}
			}
			return ret;
			*/
		}  
		public static function findColumns(matrix:classes.Matrix):Array{
			var mean:Array = AnalyzedData.calculateMean(matrix);
			var diff:Array = AnalyzedData.diff(mean,2);
			var zeros:Array = AnalyzedData.cleanupZeros(AnalyzedData.findZeros(diff,true));
			// cleanup zeros if needed
			return zeros;
		} 
		
		public function addColumns(ar:Array):void{
			for each(var x:int in ar){
				this.columns.push(new Column(this,x));
			}
			this.columns.sort(function(A:Column, B:Column):int{
				return A.x - B.x;
			},Array.NUMERIC);
			
		}
		
		public function compareStripes():void{
			this.columns.sort(function(A:Column, B:Column):int{
				return A.x - B.x;
			});
			
			for each(var column:Column in this.columns){
				
			}
		}
	}
}